package com.lxd.controller;

import java.util.List;

import com.lxd.annotaion.GlobalInterceptor;
import com.lxd.entity.UserRequest;
import com.lxd.entity.po.Club;
import com.lxd.entity.query.ClubQuery;
import com.lxd.entity.query.MemberQuery;
import com.lxd.entity.query.UserQuery;
import com.lxd.entity.po.User;
import com.lxd.entity.vo.ResponseVO;
import com.lxd.exception.BusinessException;
import com.lxd.service.ClubService;
import com.lxd.service.UserService;
import com.lxd.utils.Constants;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Controller
 */
@RestController("userController")
@RequestMapping("/user")
public class UserController extends ABaseController {

    @Resource
    private UserService userService;

    /**
     * 根据条件分页查询
     */
    @RequestMapping("/loadDataList")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO loadDataList(UserQuery query) {
        return getSuccessResponseVO(userService.findListByPage(query));
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    public ResponseVO add(User bean) {
        userService.add(bean);
        return getSuccessResponseVO(null);
    }

    /**
     * 批量新增
     */
    @RequestMapping("/addBatch")
    public ResponseVO addBatch(@RequestBody List<User> listBean) {
        userService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 批量新增/修改
     */
    @RequestMapping("/addOrUpdateBatch")
    public ResponseVO addOrUpdateBatch(@RequestBody List<User> listBean) {
        userService.addBatch(listBean);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据UserId查询对象
     */
    @RequestMapping("/getUserByUserId")
    public ResponseVO getUserByUserId(Long userId) {
        return getSuccessResponseVO(userService.getUserByUserId(userId));
    }

    /**
     * 根据UserId修改对象
     */
    @RequestMapping("/updateUserByUserId")
    public ResponseVO updateUserByUserId(User bean, Long userId) {
        userService.updateUserByUserId(bean, userId);
        return getSuccessResponseVO(null);
    }


    /**
     * 根据UserName查询对象
     */
    @RequestMapping("/getUserByUserName")
    public ResponseVO getUserByUserName(String userName) {
        return getSuccessResponseVO(userService.getUserByUserName(userName));
    }

    /**
     * 根据UserName修改对象
     */
    @RequestMapping("/updateUserByUserName")
    public ResponseVO updateUserByUserName(User bean, String userName) {
        userService.updateUserByUserName(bean, userName);
        return getSuccessResponseVO(null);
    }

    /**
     * 根据UserName删除
     */
    @RequestMapping("/deleteUserByUserName")
    public ResponseVO deleteUserByUserName(String userName) {
        userService.deleteUserByUserName(userName);
        return getSuccessResponseVO(null);
    }

    /**
     * 任命管理员
     */
    @RequestMapping("/setAdmin")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO setAdmin(@RequestBody UserRequest userRequest) {
        Long userId = userRequest.getUserId();
        User user = new User();
        user.setUserId(userId);
        user.setRole(userRequest.isSetAdmin() ? "管理员" : "学生");
        userService.updateUserByUserId(user, userId);
        return getSuccessResponseVO("已" + (userRequest.isSetAdmin() ? "任命" : "取消") + 
            userRequest.getUserRealName() + (userRequest.isSetAdmin() ? "为管理员" : "的管理员身份") + "！！！");
    }

    /**
     * 取消社长
     */
    @RequestMapping("/cancelPresident")
    public ResponseVO cancelPresident(@RequestBody UserRequest userRequest) {
        userService.cancelPresident(userRequest);
        return getSuccessResponseVO("已取消" + userRequest.getUserRealName() + "的社长身份！！！");
    }

    /**
     * 任命社长
     */
    @RequestMapping("/setPresident")
    public ResponseVO setPresident(@RequestBody UserRequest userRequest) {
        Long userId = userRequest.getUserId();
        userService.setPresident(userRequest);
        return getSuccessResponseVO("任命" + userRequest.getUserRealName() + "为社长成功！！");

    }

    /**
     * 重置密码
     */
    @RequestMapping("/resetPassword")
    public ResponseVO resetPassword(@RequestBody  UserRequest userRequest) {
        // 校验输入参数
        if (userRequest == null || userRequest.getUserId() == null) {
            throw new BusinessException("用户ID不能为空");
        }

        Long userId = userRequest.getUserId();
        String newPassWord=null;
        String email = userRequest.getEmail();
        int atIndex = email.indexOf("@");
        if (atIndex!=-1){
            newPassWord = email.substring(0, atIndex);
        }else{
            newPassWord = "123456";
        }

        User user = new User();
        user.setUserId(userId);
        user.setPassword(newPassWord); // 默认密码
        userService.updateUserByUserId(user, userId);
        return getSuccessResponseVO("重置密码成功,密码为"+newPassWord);

    }

    @RequestMapping("/deleteUser")
    public ResponseVO deleteUser(@RequestBody  UserRequest userRequest) {
        userService.deleteUser(userRequest);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/getUserInfo")
    @GlobalInterceptor(checkLogin = true)  // 需要登录才能访问
    public ResponseVO getUserInfo(HttpServletRequest request) {
        // 从请求头获取token
        String token = request.getHeader(Constants.HEADER_TOKEN);
        return getSuccessResponseVO(userService.getUserInfo(token));
    }
    @RequestMapping("/totalCount")
    public ResponseVO totalCount() {
        Integer count = userService.getTotalCount();
        return getSuccessResponseVO(count);
    }

}